package org.example;

import java.util.Stack;

class MinStack {
    Stack<Integer> st ;
    Integer temp=null;
    public MinStack() {
        st = new Stack();
    }

    public void push(int val) {
        st.push(val);
    }

    public void pop() {
        st.pop();
    }

    public int top() {
        int temp =st.pop();
        st.push(temp);
        return temp;
    }

    public int getMin() {
        if(temp==null){
            Stack<Integer> stq = new Stack();
            temp = st.pop();
            stq.push(temp);
            while(!st.empty()){
                int i =st.pop();
                temp = Math.min(temp,i);
                stq.push(i);
            }
            while(!stq.empty()){
                st.push(stq.pop());
            }
        }


        return temp;
    }
}

/**
 * Your MinStack object will be instantiated and called as such:
 * MinStack obj = new MinStack();
 * obj.push(val);
 * obj.pop();
 * int param_3 = obj.top();
 * int param_4 = obj.getMin();
 */